package external_libraries;

import javax.mail.*;
import javax.mail.internet.*;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import layout.Loader;

public class SendMail {

	public SendMail(String pass) {

		final String username = "mailatdatabase@gmail.com";
		final String password = "2Onairda0";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		Message msg = new MimeMessage(session);
		try {
			msg.setFrom(new InternetAddress("mailatdatabase@gmail.com"));
			msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
					Loader.getDatabase().getMail(), Loader.getDatabase()
							.getOwner()));
			msg.setSubject("Database - Recuperar Password");
			msg.setText("Database: Recuperação de password"
					+ "\n\nA password actual é: "
					+ pass
					+ "\n\nEsta password vai ser usada no próximo login.");
			Transport.send(msg);

		} catch (MessagingException e) {
			e.printStackTrace(); // To change body of catch statement use File |
									// Settings | File Templates.
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace(); // To change body of catch statement use File |
									// Settings | File Templates.
		}
	}
}
